
setwd(paste(curcomp, "/paper", sep = ""))

##########################
## Disaggregated inputs ##
##########################

cur <- usdat
mod1 <- glm(divided3 ~ log(rpimp0509+1) + log(totusinpw0509+1) + log(totusinpd0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)

cur <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$assposadcvd %in% c("No position") == FALSE),]; nrow(cur)
mod4a <- lm(posdivindex ~ log(rpimp0509+1) + log(totusinpw0509+1) + log(totusinpd0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod4b <- lm(lobdivindex ~ log(rpimp0509+1) + log(totusinpw0509+1) + log(totusinpd0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod4c <- lm(lobspnddivindex ~ log(rpimp0509+1) + log(totusinpw0509+1) + log(totusinpd0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mods <- list(summary2(mod1), summary2(mod4a), summary2(mod4b), summary2(mod4c))

vars <- c(rownames(summary2(mod1))[2:8], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15)] <- c("Related-party imports", "Imported inputs (homogeneous)",
  "Imported inputs (differentiated)", "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") 
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod4a)$df[2:3]), sum(summary(mod4b)$df[2:3]), sum(summary(mod4c)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(16); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/disagginputs.tex")



#########################
## Additional controls ##
#########################

# Divisions
cur <- usdat; nrow(cur) 
mod1 <- glm(divided3 ~ log(rpimp0509+1) + diff + nonrpca0509  + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur, family = binomial)
mod2 <- glm(divided3 ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur, family = binomial)

# Table A3
mods <- list(summary2(mod1), summary2(mod2)) 
vars <- c(rownames(summary2(mod1))[2],rownames(summary2(mod2))[2:14], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";   
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Elasticity", "Import penetration", "Primary", "No. assocs.", "No. firms", "20-firm concentration", "Agmt. Tariff Variation", "Tariff lines", "Intercept") # 
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(30); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divROB.tex")

# Composite indices
cur <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$asspos %in% c("No position") == FALSE),]; nrow(cur)
mod1a <- lm(I(100*posdivindex) ~ log(rpimp0509+1) + as.numeric(diff) + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur)
mod1b <- lm(I(100*posdivindex) ~ log(totusinp0509+1) + as.numeric(diff) + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur)

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- lm(I(100*lobdivindex) ~ log(rpimp0509+1) + as.numeric(diff) + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur); summary(mod2)
mod2b <- lm(I(100*lobdivindex) ~ log(totusinp0509+1) + as.numeric(diff) + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur); summary(mod2)

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- lm(I(100*lobspnddivindex) ~ log(rpimp0509+1) + as.numeric(diff) + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur)
mod3b <- lm(I(100*lobspnddivindex) ~ log(totusinp0509+1) + as.numeric(diff) + nonrpca0509 + log(sales0509) + I(imp0509/sales0509) + log(elast) + primary + numassoc + log(estab+1) + concen20 + agrcoefvar + log(agrtarnumlines+1), data = cur)

# Table A4
mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod1a))[2],rownames(summary2(mod1b))[2:13], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13,15,17,19,21,23,25,27)] <- c("Related-party imports", "Imported inputs",
  "Diff. (numeric)", "Comp. Adv.", "Sales", "Elasticity", "Import penetration", "Primary", "No. assocs.", "No. firms", "20-firm concentration", "Agmt. Tariff Variation", "Tariff lines", "Intercept") #N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]), sum(summary(mod3)$df[2:3]))
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), 
  sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(28); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindicesROB.tex")

# Divisions with country intercepts
# for(i in unique(usdat$country)){ print(c(i, sum(usdat[usdat$country == i, "divided3"])))}

cur <- usdat[(usdat$country %in% c("Chile","Jordan","MEFTA","Singapore")) == FALSE, ]; nrow(cur)
mod1 <- lm(I(100*divided3) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)
mod2 <- lm(I(100*divided3) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)

# Table A5
mods <- list(summary2(mod1), summary2(mod2)) 
vars <- c(rownames(summary2(mod1))[2],rownames(summary2(mod2))[2:6], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";   
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") # 
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divROB2.tex")

# Table A6
cur <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$asspos %in% c("No position") == FALSE),]; nrow(cur)
mod1a <- lm(I(100*posdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)
mod1b <- lm(I(100*posdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- lm(I(100*lobdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)
mod2b <- lm(I(100*lobdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- lm(I(100*lobspnddivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)
mod3b <- lm(I(100*lobspnddivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509) + country, data = cur)

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod1a))[2],rownames(summary2(mod1b))[2:6], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") #N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]), sum(summary(mod3)$df[2:3]))
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), 
  sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindicesROB2.tex")

###############################
## Lobbying splits breakdown ##
###############################

cur <- usdat[(usdat$numsupfirm >= 3 | usdat$numoppfirm >= 3 | (usdat$assposadcvd %in% c("No position") == FALSE)),]; nrow(cur) # usdat[usdat$indposadcvd != "No position",]; 
mod1 <- glm(firmsnoassoc3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod2 <- glm(assocandfirms3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod3 <- glm(assocnofirms3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mods <- list(summary2(mod1), summary2(mod2), summary2(mod3)); mods
R2 <- c(myround(pR2(mod1), 2)[6],myround(pR2(mod2), 2)[6],myround(pR2(mod3), 2)[6])

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod1a <- glm(firmslobnotassoc3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod2a <- glm(firmsandassoclob3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod3a <- glm(assoclobnotfirms3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
R2 <- c(R2, myround(pR2(mod1a), 2)[6],myround(pR2(mod2a), 2)[6],myround(pR2(mod3a), 2)[6])
mods[[4]] <- summary2(mod1a); mods[[5]] <-  summary2(mod2a); mods[[6]] <- summary2(mod3a)

## Table A7
vars <- c(rownames(summary2(mod1a))[2:7], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]), sum(summary(mod3)$df[2:3]),
  sum(summary(mod1a)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod3a)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(14); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/poslob.tex")

cur <- usdat[(usdat$numsupfirm >= 3 | usdat$numoppfirm >= 3 | (usdat$assposadcvd %in% c("No position") == FALSE)),]; nrow(cur) # usdat[usdat$indposadcvd != "No position",]; 
model = firmsnoassoc3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509) # + country2
csvar = "rpimp0509"; csquant = c(.25, .75); cs1 <- logcs2(model, cur, csvar, csquant); cs1 <- cs1[[1]]
csvar = "totusinp0509"; csquant = c(.25, .75); cs2 <- logcs2(model, cur, csvar, csquant); cs2 <- cs2[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Mod. differentiated"); cs3a <- logcs2(model, cur, csvar, csquant); cs3a <- cs3a[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Differentiated"); cs3b <- logcs2(model, cur, csvar, csquant); cs3b <- cs3b[[1]]
inf1 <- c(cs1, cs2, cs3a, cs3b)

mod <- glm(model, data = cur, family = "binomial"); vcmat <- vcov(mod); 
betas <- mvrnorm(nrow(cur), mu = coef(mod), Sigma = vcmat); mm1 <- apply(model.matrix(mod), 2, median); mm0 <- mm1; 
mm0[c("diffMod. differentiated")] <- 0; mm0[c("diffDifferentiated")] <- 0; mm0[c("log(rpimp0509 + 1)")] <- quantile(log(cur$rpimp0509+1), .25); mm0[c("log(totusinp0509 + 1)")] <- quantile(log(cur$totusinp0509+1), .25);
mm1[c("diffMod. differentiated")] <- 0; mm1[c("diffDifferentiated")] <- 1; mm1[c("log(rpimp0509 + 1)")] <- quantile(log(cur$rpimp0509+1), .75); mm1[c("log(totusinp0509 + 1)")] <- quantile(log(cur$totusinp0509+1), .75);
sup0 <- c(); sup1 <- c()
for(i in 1:nrow(betas)){cf0 <- mm0%*%betas[i,]; sup0[i] <- median(exp(cf0)); cf1 <- mm1%*%betas[i,]; sup1[i] <- median(exp(cf1))}
infALL <- c(myround(median(sup1), 3), myround(median(sup0), 3), myround(quantile((sup1-sup0), .5), 3), 
  paste("[", myround(quantile((sup1-sup0), .025), 3), ", ", myround(quantile((sup1-sup0), .975), 3), "]", sep = ""))

model = assocandfirms3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509) # + country2
csvar = "rpimp0509"; csquant = c(.25, .75); cs1 <- logcs2(model, cur, csvar, csquant); cs1 <- cs1[[1]]
csvar = "totusinp0509"; csquant = c(.25, .75); cs2 <- logcs2(model, cur, csvar, csquant); cs2 <- cs2[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Mod. differentiated"); cs3a <- logcs2(model, cur, csvar, csquant); cs3a <- cs3a[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Differentiated"); cs3b <- logcs2(model, cur, csvar, csquant); cs3b <- cs3b[[1]]
inf2 <- c(cs1, cs2, cs3a, cs3b)

model = assocnofirms3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509) # + country2
csvar = "rpimp0509"; csquant = c(.25, .75); cs1 <- logcs2(model, cur, csvar, csquant); cs1 <- cs1[[1]]
csvar = "totusinp0509"; csquant = c(.25, .75); cs2 <- logcs2(model, cur, csvar, csquant); cs2 <- cs2[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Mod. differentiated"); cs3a <- logcs2(model, cur, csvar, csquant); cs3a <- cs3a[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Differentiated"); cs3b <- logcs2(model, cur, csvar, csquant); cs3b <- cs3b[[1]]
inf3 <- c(cs1, cs2, cs3a, cs3b)
cbind(inf1, inf2, inf3)

cur <- usdat[usdat$numfirmlob >= 1 | usdat$numassoclob >= 1,]; nrow(cur)
model = firmsnoassoc3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509) # + country2
csvar = "rpimp0509"; csquant = c(.25, .75); cs1 <- logcs2(model, cur, csvar, csquant); cs1 <- cs1[[1]]
csvar = "totusinp0509"; csquant = c(.25, .75); cs2 <- logcs2(model, cur, csvar, csquant); cs2 <- cs2[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Mod. differentiated"); cs3a <- logcs2(model, cur, csvar, csquant); cs3a <- cs3a[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Differentiated"); cs3b <- logcs2(model, cur, csvar, csquant); cs3b <- cs3b[[1]]
inf1 <- c(cs1, cs2, cs3a, cs3b)

model = firmsandassoclob3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509) # + country2
csvar = "rpimp0509"; csquant = c(.25, .75); cs1 <- logcs2(model, cur, csvar, csquant); cs1 <- cs1[[1]]
csvar = "totusinp0509"; csquant = c(.25, .75); cs2 <- logcs2(model, cur, csvar, csquant); cs2 <- cs2[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Mod. differentiated"); cs3a <- logcs2(model, cur, csvar, csquant); cs3a <- cs3a[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Differentiated"); cs3b <- logcs2(model, cur, csvar, csquant); cs3b <- cs3b[[1]]
inf2 <- c(cs1, cs2, cs3a, cs3b)

model = assoclobnotfirms3 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509) # + country2
csvar = "rpimp0509"; csquant = c(.25, .75); cs1 <- logcs2(model, cur, csvar, csquant); cs1 <- cs1[[1]]
csvar = "totusinp0509"; csquant = c(.25, .75); cs2 <- logcs2(model, cur, csvar, csquant); cs2 <- cs2[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Mod. differentiated"); cs3a <- logcs2(model, cur, csvar, csquant); cs3a <- cs3a[[1]]
csvar = "diff"; csquant = c("Homogeneous", "Differentiated"); cs3b <- logcs2(model, cur, csvar, csquant); cs3b <- cs3b[[1]]
inf3 <- c(cs1, cs2, cs3a, cs3b)
cbind(inf1, inf2, inf3)


########################
## Manufacturing only ##
########################

## Divisions
cur <- usdat[usdat$sector %in% c("manu"),]; nrow(cur) 
mod1a <- glm(divided3 ~ log(rpimp0509+1) + as.numeric(diff) + nonrpca0509  + log(sales0509), data = cur, family = binomial)
mod1b <- glm(divided3 ~ log(totusinp0509+1) + as.numeric(diff) + nonrpca0509  + log(sales0509), data = cur, family = binomial)

# Table B1
mods <- list(summary2(mod1a), summary2(mod1b)) 
vars <- c(rownames(summary2(mod1a))[2],rownames(summary2(mod1b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration",
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(pR2(mod1a), 2)[6],myround(pR2(mod1b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divMANU.tex")


## Lobbying splits
exR2 <- function(mod) summary(mod1a)$adj.r.squared
cur <- usdat[usdat$sector == "manu" & (usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$asspos %in% c("No position") == FALSE),]; nrow(cur)
mod1a <- lm(I(100*posdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod1b <- glm(I(100*posdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$sector == "manu" & usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- glm(I(100*lobdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod2b <- glm(I(100*lobdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$sector == "manu" & usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- glm(I(100*lobspnddivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod3b <- glm(I(100*lobspnddivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

# Table B2
mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod3a))[2],rownames(summary2(mod3b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports","Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(exR2(mod1a), 2),myround(exR2(mod1b), 2),myround(pR2(mod2a), 2)[6],myround(pR2(mod2b), 2)[6],
  myround(pR2(mod3a), 2)[6],myround(pR2(mod3b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindicesMANU.tex")


###############
## 2-cutoffs ##
###############

usdat$firmsnoassoc2 <- (usdat$numsupfirm >= 2 | usdat$numoppfirm >= 2) & (usdat$assposadcvd == "No position")
usdat$assocnofirms2 <- (usdat$numsupfirm < 2 & usdat$numoppfirm < 2) & ((usdat$assposadcvd == "No position") == FALSE)
usdat$assocandfirms2 <-  (usdat$numsupfirm >= 2 | usdat$numoppfirm >= 2) & ((usdat$assposadcvd == "No position") == FALSE)

usdat$firmslobnotassoc2 <- (usdat$numfirmlob >= 2) & (usdat$numassoclob == 0)
usdat$assoclobnotfirms2 <- (usdat$numfirmlob < 2) & (usdat$numassoclob >= 1)
usdat$firmsandassoclob2 <-  (usdat$numfirmlob >= 2) & (usdat$numassoclob >= 1)

usdat$divided2 <- (usdat$assposadcvd %in% c("Divided")) | (usdat$numsupfirm >= 2 & (usdat$numoppfirm >=2 | usdat$assposadcvd %in% c("Divided","Oppose"))) |  (usdat$numoppfirm >= 3 & (usdat$numsupfirm >=3 | usdat$assposadcvd %in% c("Divided","Favor")))

## Divisions
cur <- usdat; nrow(cur) 
mod1 <- glm(divided2 ~ log(rpimp0509+1) + diff + nonrpca0509  + log(sales0509), data = cur, family = binomial)
mod2 <- glm(divided2 ~ log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509), data = cur, family = binomial)

mods <- list(summary2(mod1), summary2(mod2)) 
vars <- c(rownames(summary2(mod1))[2], rownames(summary2(mod2))[2:6], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod)); stars[mod[tabinmod,4] < .1] <- "^{*}"; stars[mod[tabinmod,4] < .05] <- "^{**}";
  stars[mod[tabinmod,4] < .01] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration",
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(pR2(mod1), 2)[6],myround(pR2(mod2), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(14); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/div2cutoff.tex")

## Lobbying splits
exR2 <- function(mod) summary(mod1a)$adj.r.squared
cur <- usdat[usdat$sector == "manu" & (usdat$numsupfirm >=2 | usdat$numoppfirm >=2 | usdat$asspos %in% c("No position") == FALSE),]; nrow(cur)
mod1a <- lm(I(100*posdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod1b <- glm(I(100*posdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$sector == "manu" & usdat$numfirmlob >= 2 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- glm(I(100*lobdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod2b <- glm(I(100*lobdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$sector == "manu" & usdat$numfirmlob >= 2 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- glm(I(100*lobspnddivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod3b <- glm(I(100*lobspnddivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod3a))[2],rownames(summary2(mod3b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports","Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(exR2(mod1a), 2),myround(exR2(mod1b), 2),myround(pR2(mod2a), 2)[6],myround(pR2(mod2b), 2)[6],
  myround(pR2(mod3a), 2)[6],myround(pR2(mod3b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindices2cutoff.tex")

## Lobbying splits
cur <- usdat[(usdat$numsupfirm >= 2 | usdat$numoppfirm >= 2 | (usdat$asspos %in% c("No position") == FALSE)),]; nrow(cur) 
mod1 <- glm(firmsnoassoc2 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod2 <- glm(assocandfirms2 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod3 <- glm(assocnofirms2 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mods <- list(summary2(mod1), summary2(mod2), summary2(mod3)); mods

cur <- usdat[(usdat$numfirmlob >= 2 | usdat$numassoclob >= 1),]; nrow(cur)
mod1a <- glm(firmslobnotassoc2 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod2a <- glm(firmsandassoclob2 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod3a <- glm(assoclobnotfirms2 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mods[[4]] <- summary2(mod1a); mods[[5]] <-  summary2(mod2a); mods[[6]] <- summary2(mod3a)

vars <- c(rownames(summary2(mod1a))[2:7], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod)); stars[mod[tabinmod,4] < .1] <- "^{*}"; stars[mod[tabinmod,4] < .05] <- "^{**}";
  stars[mod[tabinmod,4] < .01] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]), sum(summary(mod3)$df[2:3]),
  sum(summary(mod1a)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod3a)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(14); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/poslob2cutoff.tex")


###############
## 4-cutoffs ##
###############

usdat$firmsnoassoc4 <- (usdat$numsupfirm >= 4 | usdat$numoppfirm >= 4) & (usdat$assposadcvd == "No position")
usdat$assocnofirms4 <- (usdat$numsupfirm < 4 & usdat$numoppfirm < 4) & ((usdat$assposadcvd == "No position") == FALSE)
usdat$assocandfirms4 <-  (usdat$numsupfirm >= 4 | usdat$numoppfirm >= 4) & ((usdat$assposadcvd == "No position") == FALSE)

usdat$firmslobnotassoc4 <- (usdat$numfirmlob >= 4) & (usdat$numassoclob == 0)
usdat$assoclobnotfirms4 <- (usdat$numfirmlob < 4) & (usdat$numassoclob >= 1)
usdat$firmsandassoclob4 <-  (usdat$numfirmlob >= 4) & (usdat$numassoclob >= 1)

usdat$divided4 <- (usdat$assposadcvd %in% c("Divided")) | (usdat$numsupfirm >= 4 & (usdat$numoppfirm >=4 | usdat$assposadcvd %in% c("Divided","Oppose"))) |  (usdat$numoppfirm >= 3 & (usdat$numsupfirm >=3 | usdat$assposadcvd %in% c("Divided","Favor")))

## Divisions
cur <- usdat; nrow(cur) 
mod1 <- glm(divided4 ~ log(rpimp0509+1) + diff + nonrpca0509  + log(sales0509), data = cur, family = binomial)
mod2 <- glm(divided4 ~ log(totusinp0509+1) + diff + nonrpca0509  + log(sales0509), data = cur, family = binomial)

mods <- list(summary2(mod1), summary2(mod2)) 
vars <- c(rownames(summary2(mod1))[2], rownames(summary2(mod2))[2:6], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod)); stars[mod[tabinmod,4] < .1] <- "^{*}"; stars[mod[tabinmod,4] < .05] <- "^{**}";
  stars[mod[tabinmod,4] < .01] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration",
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(pR2(mod1), 2)[6],myround(pR2(mod2), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(14); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/div4cutoff.tex")

## Lobbying splits
exR2 <- function(mod) summary(mod1a)$adj.r.squared
cur <- usdat[usdat$sector == "manu" & (usdat$numsupfirm >=4 | usdat$numoppfirm >=4 | usdat$asspos %in% c("No position") == FALSE),]; nrow(cur)
mod1a <- lm(I(100*posdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod1b <- glm(I(100*posdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$sector == "manu" & usdat$numfirmlob >= 4 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- glm(I(100*lobdivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod2b <- glm(I(100*lobdivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

cur <- usdat[usdat$sector == "manu" & usdat$numfirmlob >= 4 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- glm(I(100*lobspnddivindex) ~ log(rpimp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)
mod3b <- glm(I(100*lobspnddivindex) ~ log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur)

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod3a))[2],rownames(summary2(mod3b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports","Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(exR2(mod1a), 2),myround(exR2(mod1b), 2),myround(pR2(mod2a), 2)[6],myround(pR2(mod2b), 2)[6],
  myround(pR2(mod3a), 2)[6],myround(pR2(mod3b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindices4cutoff.tex")

## Lobbying splits
cur <- usdat[(usdat$numsupfirm >= 4 | usdat$numoppfirm >= 4 | (usdat$asspos %in% c("No position") == FALSE)),]; nrow(cur) 
mod1 <- glm(firmsnoassoc4 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod2 <- glm(assocandfirms4 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod3 <- glm(assocnofirms4 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mods <- list(summary2(mod1), summary2(mod2), summary2(mod3)); mods

cur <- usdat[(usdat$numfirmlob >= 4 | usdat$numassoclob >= 1),]; nrow(cur)
mod1a <- glm(firmslobnotassoc4 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod2a <- glm(firmsandassoclob4 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mod3a <- glm(assoclobnotfirms4 ~ log(rpimp0509+1) + log(totusinp0509+1) + diff + nonrpca0509 + log(sales0509), data = cur, family = binomial)
mods[[4]] <- summary2(mod1a); mods[[5]] <-  summary2(mod2a); mods[[6]] <- summary2(mod3a)

vars <- c(rownames(summary2(mod1a))[2:7], "(Intercept)") 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod)); stars[mod[tabinmod,4] < .1] <- "^{*}"; stars[mod[tabinmod,4] < .05] <- "^{**}";
  stars[mod[tabinmod,4] < .01] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],3), stars, sep = ""), paste("(", myround(mod[tabinmod,2],3), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11,13)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales", "Intercept") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1)$df[2:3]), sum(summary(mod2)$df[2:3]), sum(summary(mod3)$df[2:3]),
  sum(summary(mod1a)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod3a)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
tab <- rbind(tab, c(N)); rownames(tab)[nrow(tab)] <- "N"
addtorow <- list(); addtorow$pos <- list(14); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/poslob4cutoff.tex")


################################
## Main Models with 1014 vars ##
################################

## Divisions
cur <- usdat; nrow(cur) 
mod1a <- glm(divided3 ~ log(rpimp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur, family = binomial)
mod1b <- glm(divided3 ~ log(totusinp1014+1) + diff + nonrpca1014  + log(sales1014), data = cur, family = binomial)

cur2 <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$assposadcvd %in% c("No position") == FALSE),]; nrow(cur2)
mod2a <- glm(divided3 ~ log(rpimp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur2, family = binomial)
mod2b <- glm(divided3 ~ log(totusinp1014+1) + diff + nonrpca1014  + log(sales1014), data = cur2, family = binomial)

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b)) 
vars <- c(rownames(summary2(mod1a))[2],rownames(summary2(mod1b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration",
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]),sum(summary(mod2a)$df[2:3]),sum(summary(mod2b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(pR2(mod1a), 2)[6],myround(pR2(mod1b), 2)[6],myround(pR2(mod2a), 2)[6],myround(pR2(mod2b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/div1014.tex")

## Lobbying splits
cur <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$assposadcvd %in% c("No position") == FALSE),]; nrow(cur)
mod1a <- glm(I(100*posdivindex) ~ log(rpimp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur)
mod1b <- glm(I(100*posdivindex) ~ log(totusinp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur)
R2 <- c(myround(pR2(mod1a), 2)[6], myround(pR2(mod1b), 2)[6])

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- glm(I(100*lobdivindex) ~ log(rpimp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur)
mod2b <- glm(I(100*lobdivindex) ~ log(totusinp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur)
R2 <- c(R2, myround(pR2(mod2a), 2)[6], myround(pR2(mod2b), 2)[6])

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- glm(I(100*lobspnddivindex) ~ log(rpimp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur)
mod3b <- glm(I(100*lobspnddivindex) ~ log(totusinp1014+1) + diff + nonrpca1014 + log(sales1014), data = cur)
R2 <- c(R2, myround(pR2(mod3a), 2)[6], myround(pR2(mod3b), 2)[6])

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod3a))[2],rownames(summary2(mod3b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports","Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(pR2(mod1a), 2)[6],myround(pR2(mod1b), 2)[6],myround(pR2(mod2a), 2)[6],myround(pR2(mod2b), 2)[6],myround(pR2(mod3a), 2)[6],myround(pR2(mod3b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindices1014.tex")

# before and after measures
sls0509 <- usdat$sales0509[1:403]; sls1014 <- usdat$sales1014[1:403];
aslsvec <- c(sls1014, rep(sls0509, 4), sls1014, sls0509, sls1014, sls0509, rep(NA, 403))
bslsvec <- c(sls1014, rep(sls0509, 4), sls1014, rep(sls0509, 3), rep(NA, 403))
usdat$asales <- aslsvec; usdat$bsales <- bslsvec

cur <- usdat; nrow(cur) 
mod1a <- glm(divided3 ~ brpimp + diff + nonrpca0509 + log(bsales), data = cur, family = binomial)
mod1b <- glm(divided3 ~ binpimp + diff + nonrpca0509  + log(bsales), data = cur, family = binomial)

cur2 <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$assposadcvd %in% c("No position") == FALSE),]; nrow(cur2)
mod2a <- glm(divided3 ~ brpimp + diff + nonrpca0509 + log(bsales), data = cur2, family = binomial)
mod2b <- glm(divided3 ~ binpimp + diff + nonrpca0509  + log(bsales), data = cur2, family = binomial)

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b)) 
vars <- c(rownames(summary2(mod1a))[2],rownames(summary2(mod1b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}"; 
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports", "Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration",
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]),sum(summary(mod2a)$df[2:3]),sum(summary(mod2b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- c(myround(pR2(mod1a), 2)[6],myround(pR2(mod1b), 2)[6],myround(pR2(mod2a), 2)[6],myround(pR2(mod2b), 2)[6])
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divbefore.tex")

cur <- usdat[(usdat$numsupfirm >=3 | usdat$numoppfirm >=3 | usdat$assposadcvd %in% c("No position") == FALSE) & (usdat$country %in% c("NAFTA","Jordan") == FALSE),]; nrow(cur)
mod1a <- glm(I(100*posdivindex) ~ brpimp + diff + nonrpca0509 + log(bsales), data = cur)
mod1b <- glm(I(100*posdivindex) ~ binpimp + diff + nonrpca0509  + log(bsales), data = cur)
R2 <- c(myround(pR2(mod1a), 2)[6], myround(pR2(mod1b), 2)[6])

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod2a <- glm(I(100*lobdivindex) ~ brpimp + diff + nonrpca0509 + log(bsales), data = cur)
mod2b <- glm(I(100*lobdivindex) ~ binpimp + diff + nonrpca0509  + log(bsales), data = cur)
R2 <- c(R2, myround(pR2(mod2a), 2)[6], myround(pR2(mod2b), 2)[6])

cur <- usdat[usdat$numfirmlob >= 3 | usdat$numassoclob >= 1,]; nrow(cur)
mod3a <- glm(I(100*lobspnddivindex) ~ brpimp + diff + nonrpca0509 + log(bsales), data = cur)
mod3b <- glm(I(100*lobspnddivindex) ~ binpimp + diff + nonrpca0509  + log(bsales), data = cur)
R2 <- c(R2, myround(pR2(mod3a), 2)[6], myround(pR2(mod3b), 2)[6])

mods <- list(summary2(mod1a), summary2(mod1b), summary2(mod2a), summary2(mod2b), summary2(mod3a), summary2(mod3b))
vars <- c(rownames(summary2(mod3a))[2],rownames(summary2(mod3b))[2:6]) 
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", nrow(mod[tabinmod,])); stars[mod[tabinmod,4] < .05] <- "^{*}"; stars[mod[tabinmod,4] < .01] <- "^{**}";
  stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1,3,5,7,9,11)] <- c("Related-party imports","Imported inputs",
  "Mod. differentiated", "Differentiated", "Comp. Adv.", "Sales") # , "No. assocs.", "No. firms", "4-firm concentration", "20-firm concentration", 
N <- c(sum(summary(mod1a)$df[2:3]), sum(summary(mod1b)$df[2:3]), sum(summary(mod2a)$df[2:3]), sum(summary(mod2b)$df[2:3]), sum(summary(mod3a)$df[2:3]), sum(summary(mod3b)$df[2:3]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = "")
R2 <- paste("\\multicolumn{1}{c}{", R2, "}", sep = "")
tab <- rbind(tab, c(R2), c(N)); rownames(tab)[c(nrow(tab)-1,nrow(tab))] <- c("pseudo R$^2$", "N")
addtorow <- list(); addtorow$pos <- list(12); addtorow$command <- c(paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "tables/divindicesbefore.tex")

